import {Injectable} from '@angular/core';
import {Http, Headers, Response, RequestOptions} from '@angular/http'
import {Project} from "../entity/Project";
import 'rxjs/add/operator/toPromise';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {ProjectStep} from "../entity/ProjectStep";
import {Report} from "../entity/Report";
import {CommonIp} from "../../user/entity/Ip";

@Injectable()
export class ProjectAddService extends CommonIp{
  private projectAddURL =this.userIp+"/p/project/publishProject";
  private projectUpdateURL =this.userIp+"/p/project/updateProject";

  private projectStepAddURL =this.userIp+"/p/projectStep/addProjectStep";
  private projectAddReportURL = this.userIp+"/p/report/publishReport";
  private updateProjectStep=this.userIp+"/p/projectStep/updateProjectStep"
  constructor(public http:Http){
    super();
  }
  public updateProgectStep(progectstep:ProjectStep):Promise<void>{
    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});
    return this.http.post(this.updateProjectStep,JSON.stringify(progectstep),options)
      .toPromise()
      .then(res => {
        if(res.status<200||res.status>=300){
          throw  new Error('Bad response status:'+res.status);
        }
        if(res.json().code== 200){

          $.jBox.tip("更新成功");

        }

      })
      .catch(this.handleError);


  }
  public deleteProgectStep(){

  }
  publishProject(project: Project) {
    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});
    return this.http.post(this.projectAddURL
      ,JSON.stringify(project),options)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }
  public updateProject(project: Project) {
    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});
    return this.http.put(this.projectUpdateURL
      ,JSON.stringify(project),options)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }
  private extractData(res: Response) {
    let result = res.json();
    return result || { };
  }
  private handleError (error: Response | any) {
    // In a real world app, you might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

  public saveProjectStep(projectStep: ProjectStep) {
    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});
    return this.http.post(this.projectStepAddURL
      ,JSON.stringify(projectStep),options)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  public reportProject(report: Report) {

    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});
    return this.http.post(this.projectAddReportURL,JSON.stringify(report), options).toPromise().then(this.extractData).catch(this.handleError);
  }

}
